//
//  Interface.m
//  FlashLoan
//
//  Created by mac on 2017/5/29.
//  Copyright © 2017年 上海翡鹿信息技术服务有限公司. All rights reserved.
//

#import "Interface.h"


@interface Interface ()

@property(nonatomic, strong)AFHTTPSessionManager *shareManger;
@property(nonatomic, strong)AFURLSessionManager *shareURLManger;


@end


@implementation Interface

+(Interface *)shareInstance{
    static Interface *_interface = nil;
    static dispatch_once_t onceInterfaceInitialize;
    dispatch_once(&onceInterfaceInitialize, ^{
        _interface = [[Interface alloc]init];
    });
    return _interface;
}


- (instancetype)init {
    self = [super init];
    if (self) {
        _shareManger = [AFHTTPSessionManager manager];
        _shareManger.responseSerializer = [AFJSONResponseSerializer serializer];
        _shareManger.securityPolicy = [AFSecurityPolicy defaultPolicy];
        _shareManger.securityPolicy.allowInvalidCertificates = YES;
        _shareManger.requestSerializer.timeoutInterval = 10.0;
    }
    return self;
}


#pragma mark --- public method

-(void)request:(HTTPMethod)method baseUrl:(NSString *)baseUrl urlString:(NSString *)urlStirng parameters:(id)parameters platformType:(PlatformType)type isEncrypted:(BOOL)isEncrypted finished:(void (^)(id, NSError *))finished{
    [self message_request:method urlString:[NSString stringWithFormat:@"%@%@",baseUrl,urlStirng] parameters:parameters finished:finished];
}


- (void)message_request:(HTTPMethod)method urlString:(NSString *)urlStirng parameters:(id)parameters finished:(void (^)(id responseObject, NSError *error))finished {
    
    void(^successCallback)(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) = ^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            if ([responseObject[@"code"] integerValue] == 200) {
                if ([responseObject[@"data"] isKindOfClass:[NSDictionary class]]) {
                    finished(responseObject,nil);
                }else {
                    finished(nil,nil);
                    DEF_DEBUG(@"数据格式返回错误");
                }
            }else if ([responseObject[@"code"] isEqualToString:@"1009"]) {
                // token失效
                finished(responseObject,nil);
            }else if ([responseObject[@"code"] isEqualToString:@"500"]){
                // 服务器异常
                finished(responseObject,nil);
            }else {
                finished(responseObject,nil);
            }
        }else{
            DEF_DEBUG(@"返回数据格式错误");
            finished(nil,nil);
        }
    };
    
    void(^failureCallBack)(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) = ^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (error.code == -1001) {
            [MBProgressHUD showTipMessageInView:@"请求超时"];
        }
        //错误的统一输出
        DEF_DEBUG(@"%@",error);
        finished(nil,error);
    };
    
    if (method == GET) {
        [self.shareManger GET:urlStirng parameters:parameters progress:nil success:successCallback failure:failureCallBack];
    }else {
        [self.shareManger POST:urlStirng parameters:parameters progress:nil success:successCallback failure:failureCallBack];
    }
    
}





@end




